********************************************************************************
* HETEROGENEITY: FIRST RESPONDENT / BOTH RESPOND / FIRST vs SECOND
*  -> OLS + RITEST  AND  OLS with IPW weights
********************************************************************************
clear
set more off
global dir "replication"

        if "$dir"=="ana"  {  
        global root   = "/Users/adiazescobar/Library/CloudStorage/Dropbox/GenderNorms2/Results/"
        global output = "/Users/adiazescobar/Library/CloudStorage/Dropbox/GenderNorms2/Results/"
        }   
        if "$dir"=="ana_imac"  {  
        global root   = "/Users/adaizescobar/Dropbox/GenderNorms2/Results"
        global output = "/Users/adaizescobar/Dropbox/GenderNorms2/Results"
        }   

if "$dir"=="marie"  {
    global root   = "C:\Users\Marie Boltz\Dropbox\Gender Norms2\Results\"
    global output = "C:\Users\Marie Boltz\Dropbox\Gender Norms2\Results\"
}

if "$dir"=="replication" {
    global root   = "."
    global output = "."
    global datadir = "./data"
    global codedir = "./code"
    global outdir  = "./output"
    capture mkdir "$outdir"
}

*cd "C:/Users/Marie Boltz/Dropbox/Gender Norms2/Results/Analysis/FINAL/Tables"
* ---- QJE-style graph defaults ----
set scheme s2mono
graph set window fontface "Times New Roman"
global GCOMMON graphregion(color(white)) ///
    plotregion(margin(l+8 r+8 t+8 b+8)) ///
    xlabel(, angle(0) labsize(small)) ///
    ylabel(, labsize(small)) ///
    title(, size(small)) subtitle(, size(vsmall)) ///
    note("", size(vsmall))
global GCOMB graphregion(color(white)) imargin(tiny)

****************************************
use "$datadir/dataALLWAVES.dta", clear

run "$codedir/def_samples.do"

cap drop Dfirst
gen Dfirst = D*first_resp


cap drop both_resp 
gen both_resp = cond(n_spouses_chat ==2,1,0)
replace both_resp =. if n_spouses_chat ==.
cap drop Dboth
gen Dboth = D* both_resp

cap drop DxInactF
gen DxInactF = D*inactiveF
label var DxInactF "Treatment x Wife inactive"



global reps 1000

drop if female_w3 != female & female_w3!=.


capture mkdir "C:/temp_ols"
cd "C:/temp_ols"

/*
*********************************************************************************
* Table 8. Changes in interest about wife's LFP – IPWRA (analytic SEs, main text) + OLS (appendix)
*********************************************************************************


**# Definitions

* Main sample indicator::
local samples sample1 first_only /*sample1_any sample1_alone sample1_other sample1_both*/

* Baseline belief block (used in diagnostics)
local bel2  fob2 sob2 sob2m sob2p

* "Strata" covariates used in absorb() and IPW models
local strat /*inactiveF employedF*/ fob2M

* Outcomes: comment/uncomment to include/exclude
local yvars y1 y2 y3 /*sob2m_w3 sob2_w3 sob2p_w3 y4 y5*/


* Initial control variables (diagnostics)
local xvars c1 c2_2_rp c3 c5 f6 age_dif  level_1 level_2 edu_WmoreH  inactiveM unemployedM estrato_12 estrato_3 tec_2 

local reps $reps
eststo clear
local estlist_het_m  ""
local estlist_het_w ""
local mtitles_het ""

cap drop w_*
cap drop wa_*
cap drop ws_*
**************
foreach sample in `samples' {
********************************************************************************
* IPW weights for selection (attrition weights for being in `sample')
********************************************************************************
cap drop ps_attrition 


	probit `sample' D `xvars' `bels' `strat', vce(robust)
	*probit `sample' D `xvars' `bels' `strat', vce(robust)

predict ps_attrition, pr 

gen w_`sample' = 1 / ps_attrition
quietly summ w_`sample'
local p_uncond = r(mean)
gen ws_`sample' = `p_uncond' / ps_attrition   // normalized (if needed)
gen wa_`sample' = 1                           // unweighted

* Default selection weight (you can switch to ws_ if you prefer normalized)
local weight w_`sample'
summ `weight' 
bys D : su `weight' if sample1 ==1
bys D : su `weight' ws_`sample' ps_attrition if sample1 ==1
local reps $reps

}
	
*local vars_sample3 female c5 f6 age_dif  unemployedM tec_2 sob2p	 sob2m	
local vars_sample3 `xvars' `bel2'


**************************
**** FINAL TABLE 

eststo clear
local estlist_M ""
local estlist_W ""

local mtitles_M ""
local mtitles_W "" 

********************************************************************************
* HETEROGENEITY: MEN vs WOMEN, ALL / FIRST / SECOND, OLS+RI vs IPW
********************************************************************************

foreach y of local yvars {

eststo clear
local estlist_`y' ""
local estlist_`y' ""

local mtitles_`y' ""
local mtitles_`y' "" 


    *----------------------------------------
    * Nice label for the outcome
    *----------------------------------------
    local ylab : variable label `y'
    if "`ylab'" == "" local ylab "`y'"

    di "========= HET: outcome `y' (`ylab') ========="
	
    *====================================================================
    * A. ALL 
    *====================================================================
    eststo clear

    *========== All – OLS + RI p-values ==========
    reghdfe `y' D inactiveF DxInactF `vars_sample3' ///
        if sample1 == 1 , ///
        absorb(`strat') vce(robust)
	eststo `y'_OLS_all	
    estadd ysumm, replace

    scalar p_ri_D     = .
    scalar p_ri_inact  = .
    scalar p_ri_Dinact = .

    capture noisily ritest D ///
        (_b[D]) (_b[inactiveF]) (_b[DxInactF]), ///
        cluster(id) strata(`strat') reps(`reps') : ///
        reghdfe `y' D inactiveF DxInactF `vars_sample3' ///
            if sample1 == 1 , ///
            absorb(`strat') vce(robust)

    if (_rc == 0) {
        matrix P = r(p)
        scalar p_ri_D     = round(P[1,1], .001)
        scalar p_ri_inact = round(P[1,2], .001)
        scalar p_ri_Dinact = round(P[1,3], .001)
    }

	estimates restore `y'_OLS_all
    estadd scalar p_ri_D     = p_ri_D,     replace
    estadd scalar p_ri_inact  = p_ri_inact,  replace
    estadd scalar p_ri_Dinact = p_ri_Dinact, replace
    eststo `y'_OLS_all

    *========== All – IPW ==========
    reghdfe `y' D inactiveF DxInactF `vars_sample3' ///
        if sample1 == 1  [pweight = w_sample1], ///
        absorb(`strat') vce(robust)
    estadd ysumm, replace
    eststo `y'_IPW_all
    *====================================================================
    * B. MEN
    *====================================================================

    *========== MEN – All – OLS + RI p-values ==========
    reghdfe `y' D inactiveF DxInactF `vars_sample3' ///
        if sample1 == 1 & female == 0, ///
        absorb(`strat') vce(robust)
	eststo `y'_OLS_M	
    estadd ysumm, replace

    scalar p_ri_D     = .
    scalar p_ri_inact  = .
    scalar p_ri_Dinact = .

    capture noisily ritest D ///
        (_b[D]) (_b[inactiveF]) (_b[DxInactF]), ///
        cluster(id) strata(`strat') reps(`reps') : ///
        reghdfe `y' D inactiveF DxInactF `vars_sample3' ///
            if sample1 == 1 & female == 0, ///
            absorb(`strat') vce(robust)

    if (_rc == 0) {
        matrix P = r(p)
        scalar p_ri_D     = round(P[1,1], .001)
        scalar p_ri_inact = round(P[1,2], .001)
        scalar p_ri_Dinact = round(P[1,3], .001)
    }

	estimates restore `y'_OLS_M
    estadd scalar p_ri_D     = p_ri_D,     replace
    estadd scalar p_ri_inact  = p_ri_inact,  replace
    estadd scalar p_ri_Dinact = p_ri_inact, replace
    eststo `y'_OLS_M

    *========== MEN – All – IPW ==========
    reghdfe `y' D inactiveF DxInactF `vars_sample3' ///
        if sample1 == 1 & female == 0 [pweight = w_sample1], ///
        absorb(`strat') vce(robust)
    estadd ysumm, replace
    eststo `y'_IPW_M


     *====================================================================
    * C. WOMEN
    *====================================================================
    *========== WOMEN – All – OLS + RI p-values ==========
    reghdfe `y' D inactiveF DxInactF `vars_sample3' ///
        if sample1 == 1 & female == 1, ///
        absorb(`strat') vce(robust)
	eststo `y'_OLS_F	
    estadd ysumm, replace

    scalar p_ri_D     = .
    scalar p_ri_inact  = .
    scalar p_ri_Dinact = .

    capture noisily ritest D ///
        (_b[D]) (_b[inactiveF]) (_b[DxInactF]), ///
        cluster(id) strata(`strat') reps(`reps') : ///
        reghdfe `y' D inactiveF DxInactF `vars_sample3' ///
            if sample1 == 1 & female == 1, ///
            absorb(`strat') vce(robust)

    if (_rc == 0) {
        matrix P = r(p)
        scalar p_ri_D     = round(P[1,1], .001)
        scalar p_ri_inact = round(P[1,2], .001)
        scalar p_ri_Dinact = round(P[1,3], .001)
    }

	estimates restore `y'_OLS_F
    estadd scalar p_ri_D     = p_ri_D,     replace
    estadd scalar p_ri_inact  = p_ri_inact,  replace
    estadd scalar p_ri_Dinact = p_ri_Dinact, replace
    eststo `y'_OLS_F

    *========== WOMEN – All – IPW ==========
    reghdfe `y' D inactiveF DxInactF `vars_sample3' ///
        if sample1 == 1 & female == 1 [pweight = w_sample1], ///
        absorb(`strat') vce(robust)
    estadd ysumm, replace
    eststo `y'_IPW_F
	   
*****************************************************************************************************	

    *---------- Export table for MEN, outcome `y' ----------
    local estlist_`y' "`y'_OLS_all `y'_IPW_all `y'_OLS_M `y'_IPW_M `y'_OLS_F `y'_IPW_F"
    local mtitles_`y' `" "All (OLS)" "All (IPW)" "Men (OLS)" "Men (IPW)" "Women (OLS)" "Women (IPW)" "'

    #delimit ;
    local opts_het_M style(tex)
      varlabels(D         "Treatment"
                inactiveF "Wife inactive"
                DxInactF     "D $\times$ Wife inactive")
      cells(b(star fmt(%9.3f)) se(par))
      stats(N ymean p_ri_D p_ri_inact p_ri_Dinact,
            fmt(%9.0f %9.3f %9.3f %9.3f %9.3f)
            label("N"
                  "Mean Dep. Var"
                  "{Fisher p-val D (OLS cols)}"
                  "{Fisher p-val Inact. W (OLS)}"
                  "{Fisher p-val D$\times$Inact (OLS)}"))
      keep(D inactiveF DxInactF)
      label collabels(none) msign(--) nolz varwidth(18) modelwidth(18)
      starlevels(* 0.10 ** 0.05 *** 0.01)
    ;
    #delimit cr

    estout `estlist_`y'' using ///
        "$outdir/HET_inactive_`y'.tex", ///
        replace `opts_het_M' mlabels(`mtitles_`y'')

}


*********************************************************************************
* Table 6. Changes in societal beliefs – IPWRA (analytic SEs, main text) + OLS (appendix)
*********************************************************************************
*gen metas_laboral_2_w3 metas_laboral_w3_2
*gen metas_laboral_3_w3 metas_laboral_w3_3



**# Definitions

* Main sample indicator::
local samples sample3_ext /*sample1_any sample1_alone sample1_other sample1_both*/

* Baseline belief block (used in diagnostics)
local bel2  fob2 sob2 sob2m sob2p

* "Strata" covariates used in absorb() and IPW models
local strat /*inactiveF employedF*/ fob2M

* Outcomes: comment/uncomment to include/exclude
local yvars fob2 sob2 sob2m sob2p job_search_123 /* metas_laboral_2 metas_laboral_3 sob2m_w3 sob2_w3 sob2p_w3 y4 y5*/


* Initial control variables (diagnostics)
local xvars c1 c2_2_rp c3 c5 f6 age_dif  level_1 level_2 edu_WmoreH  inactiveM unemployedM estrato_12 estrato_3 tec_2 

local reps $reps
eststo clear
local estlist_het_m  ""
local estlist_het_w ""
local mtitles_het ""

cap drop w_*
cap drop wa_*
cap drop ws_*
**************
foreach sample in `samples' {
********************************************************************************
* IPW weights for selection (attrition weights for being in `sample')
********************************************************************************
cap drop ps_attrition 


	probit `sample' D `xvars' `bel2' `strat', vce(robust)
	*probit `sample' D `xvars' `bels' `strat', vce(robust)

predict ps_attrition, pr 

gen w_`sample' = 1 / ps_attrition
quietly summ w_`sample'
local p_uncond = r(mean)
gen ws_`sample' = `p_uncond' / ps_attrition   // normalized (if needed)
gen wa_`sample' = 1                           // unweighted

* Default selection weight (you can switch to ws_ if you prefer normalized)
local weight w_`sample'
summ `weight' 
bys D : su `weight' if sample1 ==1
bys D : su `weight' ws_`sample' ps_attrition if sample1 ==1
local reps $reps

}
	
*local vars_sample3 female c5 f6 age_dif  unemployedM tec_2 sob2p	 sob2m	
local vars_sample3 `xvars' `bel2'


**************************
**** FINAL TABLE 

eststo clear
local estlist_M ""
local estlist_W ""

local mtitles_M ""
local mtitles_W "" 

********************************************************************************
* HETEROGENEITY: MEN vs WOMEN, ALL / FIRST / SECOND, OLS+RI vs IPW
********************************************************************************

foreach y of local yvars {

eststo clear
local estlist_`y' ""
local estlist_`y' ""

local mtitles_`y' ""
local mtitles_`y' "" 


    *----------------------------------------
    * Nice label for the outcome
    *----------------------------------------
    local ylab : variable label `y'
    if "`ylab'" == "" local ylab "`y'"

    di "========= HET: outcome `y' (`ylab') ========="
	
    *====================================================================
    * A. ALL 
    *====================================================================
    eststo clear

    *========== All – OLS + RI p-values ==========
    reghdfe `y'_w3 D inactiveF DxInactF `y' `vars_sample3' ///
        if sample3_ext == 1 , ///
        absorb(`strat') vce(robust)
	eststo `y'_OLS_all	
    estadd ysumm, replace

    scalar p_ri_D     = .
    scalar p_ri_inact  = .
    scalar p_ri_Dinact = .

    capture noisily ritest D ///
        (_b[D]) (_b[inactiveF]) (_b[DxInactF]), ///
        cluster(id) strata(`strat') reps(`reps') : ///
        reghdfe `y'_w3 D inactiveF DxInactF  `y' `vars_sample3' ///
            if sample3_ext == 1 , ///
            absorb(`strat') vce(robust)

    if (_rc == 0) {
        matrix P = r(p)
        scalar p_ri_D     = round(P[1,1], .001)
        scalar p_ri_inact = round(P[1,2], .001)
        scalar p_ri_Dinact = round(P[1,3], .001)
    }

	estimates restore `y'_OLS_all
    estadd scalar p_ri_D     = p_ri_D,     replace
    estadd scalar p_ri_inact  = p_ri_inact,  replace
    estadd scalar p_ri_Dinact = p_ri_Dinact, replace
    eststo `y'_OLS_all

    *========== All – IPW ==========
    reghdfe `y'_w3 D inactiveF DxInactF  `y' `vars_sample3' ///
        if sample3_ext == 1  [pweight = w_sample3_ext], ///
        absorb(`strat') vce(robust)
    estadd ysumm, replace
    eststo `y'_IPW_all
    *====================================================================
    * B. MEN
    *====================================================================

    *========== MEN – All – OLS + RI p-values ==========
    reghdfe `y'_w3 D inactiveF DxInactF  `y' `vars_sample3' ///
        if sample3_ext == 1 & female == 0, ///
        absorb(`strat') vce(robust)
	eststo `y'_OLS_M	
    estadd ysumm, replace

    scalar p_ri_D     = .
    scalar p_ri_inact  = .
    scalar p_ri_Dinact = .

    capture noisily ritest D ///
        (_b[D]) (_b[inactiveF]) (_b[DxInactF]), ///
        cluster(id) strata(`strat') reps(`reps') : ///
        reghdfe `y'_w3  D inactiveF DxInactF  `y' `vars_sample3' ///
            if sample3_ext == 1 & female == 0, ///
            absorb(`strat') vce(robust)

    if (_rc == 0) {
        matrix P = r(p)
        scalar p_ri_D     = round(P[1,1], .001)
        scalar p_ri_inact = round(P[1,2], .001)
        scalar p_ri_Dinact = round(P[1,3], .001)
    }

	estimates restore `y'_OLS_M
    estadd scalar p_ri_D     = p_ri_D,     replace
    estadd scalar p_ri_inact  = p_ri_inact,  replace
    estadd scalar p_ri_Dinact = p_ri_inact, replace
    eststo `y'_OLS_M

    *========== MEN – All – IPW ==========
    reghdfe `y'_w3 D inactiveF DxInactF  `y' `vars_sample3' ///
        if sample3_ext == 1 & female == 0 [pweight = w_sample3_ext], ///
        absorb(`strat') vce(robust)
    estadd ysumm, replace
    eststo `y'_IPW_M


     *====================================================================
    * C. WOMEN
    *====================================================================
    *========== WOMEN – All – OLS + RI p-values ==========
    reghdfe `y'_w3 D inactiveF DxInactF `y' `vars_sample3' ///
        if sample3_ext == 1 & female == 1, ///
        absorb(`strat') vce(robust)
	eststo `y'_OLS_F	
    estadd ysumm, replace

    scalar p_ri_D     = .
    scalar p_ri_inact  = .
    scalar p_ri_Dinact = .

    capture noisily ritest D ///
        (_b[D]) (_b[inactiveF]) (_b[DxInactF]), ///
        cluster(id) strata(`strat') reps(`reps') : ///
        reghdfe `y'_w3 D inactiveF DxInactF   `y' `vars_sample3' ///
            if sample3_ext == 1 & female == 1, ///
            absorb(`strat') vce(robust)

    if (_rc == 0) {
        matrix P = r(p)
        scalar p_ri_D     = round(P[1,1], .001)
        scalar p_ri_inact = round(P[1,2], .001)
        scalar p_ri_Dinact = round(P[1,3], .001)
    }

	estimates restore `y'_OLS_F
    estadd scalar p_ri_D     = p_ri_D,     replace
    estadd scalar p_ri_inact  = p_ri_inact,  replace
    estadd scalar p_ri_Dinact = p_ri_Dinact, replace
    eststo `y'_OLS_F

    *========== WOMEN – All – IPW ==========
    reghdfe `y'_w3 D inactiveF DxInactF  `y' `vars_sample3' ///
        if sample3_ext == 1 & female == 1 [pweight = w_sample3_ext], ///
        absorb(`strat') vce(robust)
    estadd ysumm, replace
    eststo `y'_IPW_F
	   
*****************************************************************************************************	

    *---------- Export table for MEN, outcome `y' ----------
    local estlist_`y' "`y'_OLS_all `y'_IPW_all `y'_OLS_M `y'_IPW_M `y'_OLS_F `y'_IPW_F"
    local mtitles_`y' `" "All (OLS)" "All (IPW)" "Men (OLS)" "Men (IPW)" "Women (OLS)" "Women (IPW)" "'

    #delimit ;
    local opts_het_M style(tex)
      varlabels(D         "Treatment"
                inactiveF "Wife inactive"
                DxInactF     "D $\times$ Wife inactive")
      cells(b(star fmt(%9.3f)) se(par))
      stats(N ymean p_ri_D p_ri_inact p_ri_Dinact,
            fmt(%9.0f %9.3f %9.3f %9.3f %9.3f)
            label("N"
                  "Mean Dep. Var"
                  "{Fisher p-val D (OLS cols)}"
                  "{Fisher p-val Inact. W (OLS)}"
                  "{Fisher p-val D$\times$Inact (OLS)}"))
      keep(D inactiveF DxInactF)
      label collabels(none) msign(--) nolz varwidth(18) modelwidth(18)
      starlevels(* 0.10 ** 0.05 *** 0.01)
    ;
    #delimit cr

    estout `estlist_`y'' using ///
        "$outdir/HET_inactive_`y'.tex", ///
        replace `opts_het_M' mlabels(`mtitles_`y'')

}
 */
 
*********************************************************************************
* Table 6. Changes in societal beliefs – IPWRA (analytic SEs, main text) + OLS (appendix)
*********************************************************************************
*gen metas_laboral_2_w3 metas_laboral_w3_2
*gen metas_laboral_3_w3 metas_laboral_w3_3



**# Definitions

* Main sample indicator::
local samples sample3_ext /*sample1_any sample1_alone sample1_other sample1_both*/

* Baseline belief block (used in diagnostics)
local bel2  fob2 sob2 sob2m sob2p

* "Strata" covariates used in absorb() and IPW models
local strat /*inactiveF employedF*/ fob2M

* Outcomes: comment/uncomment to include/exclude
local yvars metas_laboral_w3_2 metas_laboral_w3_3 

* Initial control variables (diagnostics)
local xvars c1 c2_2_rp c3 c5 f6 age_dif  level_1 level_2 edu_WmoreH  inactiveM unemployedM estrato_12 estrato_3 tec_2 

local reps $reps
eststo clear
local estlist_het_m  ""
local estlist_het_w ""
local mtitles_het ""

cap drop w_*
cap drop wa_*
cap drop ws_*
**************
foreach sample in `samples' {
********************************************************************************
* IPW weights for selection (attrition weights for being in `sample')
********************************************************************************
cap drop ps_attrition 


	probit `sample' D `xvars' `bel2' `strat', vce(robust)
	*probit `sample' D `xvars' `bels' `strat', vce(robust)

predict ps_attrition, pr 

gen w_`sample' = 1 / ps_attrition
quietly summ w_`sample'
local p_uncond = r(mean)
gen ws_`sample' = `p_uncond' / ps_attrition   // normalized (if needed)
gen wa_`sample' = 1                           // unweighted

* Default selection weight (you can switch to ws_ if you prefer normalized)
local weight w_`sample'
summ `weight' 
bys D : su `weight' if sample1 ==1
bys D : su `weight' ws_`sample' ps_attrition if sample1 ==1
local reps $reps

}
	
*local vars_sample3 female c5 f6 age_dif  unemployedM tec_2 sob2p	 sob2m	
local vars_sample3 `xvars' `bel2'


**************************
**** FINAL TABLE 

eststo clear
local estlist_M ""
local estlist_W ""

local mtitles_M ""
local mtitles_W "" 

********************************************************************************
* HETEROGENEITY: MEN vs WOMEN, ALL / FIRST / SECOND, OLS+RI vs IPW
********************************************************************************

foreach y of local yvars {

eststo clear
local estlist_`y' ""
local estlist_`y' ""

local mtitles_`y' ""
local mtitles_`y' "" 


    *----------------------------------------
    * Nice label for the outcome
    *----------------------------------------
    local ylab : variable label `y'
    if "`ylab'" == "" local ylab "`y'"

    di "========= HET: outcome `y' (`ylab') ========="
	
    *====================================================================
    * A. ALL 
    *====================================================================
    eststo clear

    *========== All – OLS + RI p-values ==========
    reghdfe `y' D inactiveF DxInactF `vars_sample3' ///
        if sample3_ext == 1 , ///
        absorb(`strat') vce(robust)
	eststo `y'_OLS_all	
    estadd ysumm, replace

    scalar p_ri_D     = .
    scalar p_ri_inact  = .
    scalar p_ri_Dinact = .

    capture noisily ritest D ///
        (_b[D]) (_b[inactiveF]) (_b[DxInactF]), ///
        cluster(id) strata(`strat') reps(`reps') : ///
        reghdfe `y' D inactiveF DxInactF  `vars_sample3' ///
            if sample3_ext == 1 , ///
            absorb(`strat') vce(robust)

    if (_rc == 0) {
        matrix P = r(p)
        scalar p_ri_D     = round(P[1,1], .001)
        scalar p_ri_inact = round(P[1,2], .001)
        scalar p_ri_Dinact = round(P[1,3], .001)
    }

	estimates restore `y'_OLS_all
    estadd scalar p_ri_D     = p_ri_D,     replace
    estadd scalar p_ri_inact  = p_ri_inact,  replace
    estadd scalar p_ri_Dinact = p_ri_Dinact, replace
    eststo `y'_OLS_all

    *========== All – IPW ==========
    reghdfe `y' D inactiveF DxInactF  `vars_sample3' ///
        if sample3_ext == 1  [pweight = w_sample3_ext], ///
        absorb(`strat') vce(robust)
    estadd ysumm, replace
    eststo `y'_IPW_all
    *====================================================================
    * B. MEN
    *====================================================================

    *========== MEN – All – OLS + RI p-values ==========
    reghdfe `y' D inactiveF DxInactF `vars_sample3' ///
        if sample3_ext == 1 & female == 0, ///
        absorb(`strat') vce(robust)
	eststo `y'_OLS_M	
    estadd ysumm, replace

    scalar p_ri_D     = .
    scalar p_ri_inact  = .
    scalar p_ri_Dinact = .

    capture noisily ritest D ///
        (_b[D]) (_b[inactiveF]) (_b[DxInactF]), ///
        cluster(id) strata(`strat') reps(`reps') : ///
        reghdfe `y'  D inactiveF DxInactF  `vars_sample3' ///
            if sample3_ext == 1 & female == 0, ///
            absorb(`strat') vce(robust)

    if (_rc == 0) {
        matrix P = r(p)
        scalar p_ri_D     = round(P[1,1], .001)
        scalar p_ri_inact = round(P[1,2], .001)
        scalar p_ri_Dinact = round(P[1,3], .001)
    }

	estimates restore `y'_OLS_M
    estadd scalar p_ri_D     = p_ri_D,     replace
    estadd scalar p_ri_inact  = p_ri_inact,  replace
    estadd scalar p_ri_Dinact = p_ri_inact, replace
    eststo `y'_OLS_M

    *========== MEN – All – IPW ==========
    reghdfe `y' D inactiveF DxInactF `vars_sample3' ///
        if sample3_ext == 1 & female == 0 [pweight = w_sample3_ext], ///
        absorb(`strat') vce(robust)
    estadd ysumm, replace
    eststo `y'_IPW_M


     *====================================================================
    * C. WOMEN
    *====================================================================
    *========== WOMEN – All – OLS + RI p-values ==========
    reghdfe `y' D inactiveF DxInactF `vars_sample3' ///
        if sample3_ext == 1 & female == 1, ///
        absorb(`strat') vce(robust)
	eststo `y'_OLS_F	
    estadd ysumm, replace

    scalar p_ri_D     = .
    scalar p_ri_inact  = .
    scalar p_ri_Dinact = .

    capture noisily ritest D ///
        (_b[D]) (_b[inactiveF]) (_b[DxInactF]), ///
        cluster(id) strata(`strat') reps(`reps') : ///
        reghdfe `y' D inactiveF DxInactF  `vars_sample3' ///
            if sample3_ext == 1 & female == 1, ///
            absorb(`strat') vce(robust)

    if (_rc == 0) {
        matrix P = r(p)
        scalar p_ri_D     = round(P[1,1], .001)
        scalar p_ri_inact = round(P[1,2], .001)
        scalar p_ri_Dinact = round(P[1,3], .001)
    }

	estimates restore `y'_OLS_F
    estadd scalar p_ri_D     = p_ri_D,     replace
    estadd scalar p_ri_inact  = p_ri_inact,  replace
    estadd scalar p_ri_Dinact = p_ri_Dinact, replace
    eststo `y'_OLS_F

    *========== WOMEN – All – IPW ==========
    reghdfe `y' D inactiveF DxInactF  `vars_sample3' ///
        if sample3_ext == 1 & female == 1 [pweight = w_sample3_ext], ///
        absorb(`strat') vce(robust)
    estadd ysumm, replace
    eststo `y'_IPW_F
	   
*****************************************************************************************************	

    *---------- Export table for MEN, outcome `y' ----------
    local estlist_`y' "`y'_OLS_all `y'_IPW_all `y'_OLS_M `y'_IPW_M `y'_OLS_F `y'_IPW_F"
    local mtitles_`y' `" "All (OLS)" "All (IPW)" "Men (OLS)" "Men (IPW)" "Women (OLS)" "Women (IPW)" "'

    #delimit ;
    local opts_het_M style(tex)
      varlabels(D         "Treatment"
                inactiveF "Wife inactive"
                DxInactF     "D $\times$ Wife inactive")
      cells(b(star fmt(%9.3f)) se(par))
      stats(N ymean p_ri_D p_ri_inact p_ri_Dinact,
            fmt(%9.0f %9.3f %9.3f %9.3f %9.3f)
            label("N"
                  "Mean Dep. Var"
                  "{Fisher p-val D (OLS cols)}"
                  "{Fisher p-val Inact. W (OLS)}"
                  "{Fisher p-val D$\times$Inact (OLS)}"))
      keep(D inactiveF DxInactF)
      label collabels(none) msign(--) nolz varwidth(18) modelwidth(18)
      starlevels(* 0.10 ** 0.05 *** 0.01)
    ;
    #delimit cr

    estout `estlist_`y'' using ///
        "$outdir/HET_inactive_`y'.tex", ///
        replace `opts_het_M' mlabels(`mtitles_`y'')

} 
 